package com.caimiao.miao.page.welcome;

import android.content.Context;

import com.caimiao.miao.bean.LocationInfo;
import com.caimiao.miao.bean.Response;
import com.caimiao.miao.bean.Device;
import com.caimiao.miao.http.HttpUtil;
import com.caimiao.miao.interfaces.ICallback;
import com.caimiao.miao.interfaces.ILocUtil;
import com.caimiao.miao.library.database.table.DeviceDBHelper;
import com.caimiao.miao.library.database.DBHelperFactory;
import com.caimiao.miao.library.utils.BaiduLocUtil;
import com.caimiao.miao.library.utils.storage.PrefUtil;
import com.caimiao.miao.library.utils.sys.BatteryUtil;
import com.caimiao.miao.library.utils.sys.DeviceUtil;
import com.caimiao.miao.library.utils.tip.LogUtil;
import com.caimiao.miao.library.utils.sys.NetUtil;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.ObservableSource;
import io.reactivex.functions.Function;


public class MainModel implements IMainContact.Model {
    private Device mDeviceInfo;

    @Override
    public void getLocation(Context context, ICallback<LocationInfo> callback) {
        new BaiduLocUtil(context).setOnLocationListener(new ILocUtil.LocationListener() {
            @Override
            public void call(LocationInfo locInfo) {
                callback.success(locInfo);
            }
        }).start();
    }

    @Override
    public void setDeviceInfo(Context context, final LocationInfo locInfo) {
        mDeviceInfo = new Device();
        mDeviceInfo.setLatitude(locInfo.getLat());
        mDeviceInfo.setLongitude(locInfo.getLng());
        mDeviceInfo.setLocType(locInfo.getType());
        mDeviceInfo.setDeviceId(DeviceUtil.getDeviceId(context));
        mDeviceInfo.setNetStatus(NetUtil.isWifiEnable(context) ? "1" : NetUtil.isMobileEnable(context) ? "2" : "0");
        mDeviceInfo.setBatteryStatus(BatteryUtil.isConnect(context) ? "1" : "0");
        LogUtil.e("MAIN", "info: " + mDeviceInfo.toString());
    }

    private Observable<Response> postInitDevice() {
        return HttpUtil
                .postInitDevice(mDeviceInfo.getDeviceId())
                .flatMap((Function<Response, ObservableSource<Response>>) response -> postUpdateDevice());
    }

    private Observable<Response> postUpdateDevice() {
        return HttpUtil.postUpdateDevice(mDeviceInfo);
    }

    @Override
    public boolean hasInit(Context context) {
//        return PrefUtil.isInitDevice();
        return false;
    }

    @Override
    public Observable<Response> postDevice(Context context) {
        return Observable.create(new ObservableOnSubscribe<Boolean>() {
            @Override
            public void subscribe(ObservableEmitter<Boolean> emitter) throws Exception {
                emitter.onNext(hasInit(context));
            }
        }).flatMap(new Function<Boolean, ObservableSource<Response>>() {
            @Override
            public ObservableSource<Response> apply(Boolean aBoolean) throws Exception {
                if (!aBoolean)
                    return postInitDevice();
                else
                    return postUpdateDevice();
            }
        });
    }

    @Override
    public void backup(Context context) {
//        PrefUtil.setInitDevice(true);
    }
}
